<?php

namespace app\api\controller;

use app\common\model\tang\Polling as pollingModel;
use app\common\model\tang\ReleaseUser;

class Polling extends Base
{
    /**
     * 巡检上报
     */
    public function upPolling()
    {
        $merchant_id = $this->request->param('merchant_id');
        $img = $this->request->param('img/a');
        $text = $this->request->param('text');
        if (empty($img) || empty($text)) {
            return SendError('请上传图片和描述');
        }
        if(is_array($img)){
            $img = implode(',',$img);
        }
        $nameInfo = ReleaseUser::where('mobile', $this->mobile)->find();
        $param['merchant_id'] =$merchant_id;
        $param['img'] = $img;
        $param['text'] = $text;
        $param['up_id'] = $nameInfo['id'];
        $param['up_name'] = $nameInfo['name'];
        $param['create_time'] = time();
        $result = (new pollingModel())->save($param);
        if ($result) {
            return SendSuccess('success', '');
        } else {
            return SendError('系统繁忙,请稍后再试');
        }
    }

    /**
     * 查看自己上报列表
     */
    public function PollingList()
    {
        $param = $this->request->param();
        if ($param['type']==1) {
            $result = (new pollingModel())->where('up_id', $this->user_id)->with('merchant')->order('create_time desc')->select();
        }else{
            $result = (new pollingModel())->with('merchant')->order('create_time desc')->select();
        }
        if ($result->isEmpty()) {
            return SendSuccess('success', []);
        } else {
            $result = $result->toArray();
            foreach ($result as $h => $v) {
                $result[$h]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                $result[$h]['img'] = explode(',',$v['img']);
            }
            return SendSuccess('success', $result);
        }
    }
}